import type { IState } from './type'
import type {
  articleDisplay,
  cpInfo,
  selectInfo,
  selectOtherInfo,
  uploadAvatar,
  uploadPublish
} from '@/service/api/profile/type'
import { defineStore } from 'pinia'
import {
  cpInfoApi,
  selectFileApi,
  selectInfoApi,
  preSelectListApi,
  uploadPublishApi,
  articleDisplayApi,
  uploadAvatarApi,
  uploadImageApi,
  selectOtherInfoApi
} from '@/service/api'
import { iscode } from '@/utils'

const useProfileStore = defineStore('profile', {
  state: (): IState => ({
    isEdit: false,
    isEditPassword: false,
    isEditArticle: false,
    articleList: [],
    modelList: [],
    userInfo: {
      add_time: '',
      address: '',
      avatar: null,
      code: null,
      gender: 0,
      id: 0,
      last_login_time: '',
      mail: null,
      nickname: '',
      pageNum: null,
      pageSize: null,
      password: '',
      password1: null,
      phone: '',
      salt: '',
      secret: null,
      title: null,
      token: null,
      url_one: null,
      url_two: null,
      verifyCode: null
    },
    othersInfo: {
      add_time: '',
      address: '',
      avatar: null,
      code: null,
      gender: 0,
      id: 0,
      last_login_time: '',
      mail: null,
      nickname: '',
      pageNum: null,
      pageSize: null,
      password: '',
      password1: null,
      phone: '',
      salt: '',
      secret: null,
      title: null,
      token: null,
      url_one: null,
      url_two: null,
      verifyCode: null
    },
    othersArticleList: [],
    othersModelList: [],
    isProfileChange: false
  }),
  actions: {
    // 获取用户信息
    async selectInfoAction(parms: selectInfo.IParms, cb?: (cbRes: selectInfo.IRes) => void) {
      const res = await selectInfoApi(parms)
      if (iscode(res)) {
        this.userInfo = res.result
        cb && cb(res)
      }
    },
    // 修改用户信息
    async cpInfoAction(parms: cpInfo.IParms, cb?: (cbRes: cpInfo.IRes) => void) {
      const res = await cpInfoApi(parms)
      if (iscode(res, true)) {
        this.selectInfoAction({})
        cb && cb(res)
      }
    },
    // 获取用户文章列表
    async selectFileAction() {
      const res = await selectFileApi()
      if (iscode(res)) {
        this.articleList = res.result
      }
    },
    // 获取用户模型列表
    async selectModelAction() {},
    // 发布操作
    async uploadPublishAction(
      parms: uploadPublish.IParms,
      cb?: (cbRes: uploadPublish.IRes) => void
    ) {
      const res = await uploadPublishApi(parms)
      if (iscode(res, true)) {
        cb && cb(res)
      }
    },
    // 获取固定标签列表
    async preSelectListAction(cb?: (cbRes: selectInfo.IRes) => void) {
      const res = await preSelectListApi()
      if (iscode(res)) {
        cb && cb(res)
      }
    },
    // 获取文章详情
    async articleDisplayAction(
      parms: articleDisplay.IParms,
      cb?: (cbRes: articleDisplay.IRes) => void
    ) {
      const res = await articleDisplayApi(parms)
      if (iscode(res)) {
        cb && cb(res)
      }
    },
    // 上传头像
    async uploadAvatarAction(parms: uploadAvatar.TParms, cb?: (cbRes: uploadAvatar.IRes) => void) {
      const res = await uploadAvatarApi(parms)
      if (iscode(res, true)) {
        this.selectInfoAction({})
        cb && cb(res)
      }
    },
    // 上传图片
    async uploadImageAction(parms: uploadAvatar.TParms, cb?: (cbRes: uploadAvatar.IRes) => void) {
      const res = await uploadImageApi(parms)
      if (iscode(res, true)) {
        cb && cb(res)
      }
    },
    // 获取其他用户的信息（包括用户信息、文章、模型等）
    async selectOtherInfoAction(
      parms: selectOtherInfo.IParms,
      cb?: (cbRes: selectOtherInfo.IRes) => void
    ) {
      const res = await selectOtherInfoApi(parms)
      if (iscode(res)) {
        const { user, file_list, model_list } = res.result
        this.othersInfo = user ?? {}
        this.othersArticleList = file_list ?? []
        this.othersModelList = model_list ?? []
        cb && cb(res)
      }
    }
  },
  persist: {
    storage: localStorage,
    paths: ['userInfo']
  }
})

export default useProfileStore
